;(function($){
	$.extend($.jgrid,{
		/**
		 *	TODO:复制 jqGrid 原生的 $.jgrid.checkValues 方法的实现,进行扩展,实际不重写;
		 *	原生checkValues：对单个列在填写数据时会做立即校验;(非空、日期、邮件...)所有校验方式,其中一个不通过都不能填写;
		 *	扩展validateCellValues：对提交的数据进行校验,(非空)是必须填写的字段,不通过将不能提交数据;其它(数字、日期、邮件...)校验方式,如果不要求非空将可以提交.
		 */
		validateCellValues : function(val, valref,g, customobject, nam) {
			var edtrul,i, nm, dft, len;
			if(typeof(customobject) === "undefined") {
				if(typeof(valref)=='string'){
					for( i =0, len=g.p.colModel.length;i<len; i++){
						if(g.p.colModel[i].name==valref) {
							edtrul = g.p.colModel[i].editrules;
							valref = i;
							try { nm = g.p.colModel[i].formoptions.label; } catch (e) {}
							break;
						}
					}
				} else if(valref >=0) {
					edtrul = g.p.colModel[valref].editrules;
				}
			} else {
				edtrul = customobject;
				nm = nam===undefined ? "_" : nam;
			}
			if(edtrul) {
				if(!nm) { nm = g.p.colNames[valref]; }
				if(edtrul.required === true) {
					if( $.jgrid.isEmpty(val) )  { return [false,nm+": "+$.jgrid.edit.msg.required,""]; }
				}
				//TODO:begin 不强制非空
				// no force required
				var rqfield = edtrul.required === true ? true : false;
				//TODO:end 
				if(edtrul.number === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
						if(isNaN(val)) { return [false,nm+": "+$.jgrid.edit.msg.number,""]; }
					}
				}
				if(typeof edtrul.minValue != 'undefined' && !isNaN(edtrul.minValue)) {
					if (parseFloat(val) < parseFloat(edtrul.minValue) ) { return [false,nm+": "+$.jgrid.edit.msg.minValue+" "+edtrul.minValue,""];}
				}
				if(typeof edtrul.maxValue != 'undefined' && !isNaN(edtrul.maxValue)) {
					if (parseFloat(val) > parseFloat(edtrul.maxValue) ) { return [false,nm+": "+$.jgrid.edit.msg.maxValue+" "+edtrul.maxValue,""];}
				}
				var filter;
				if(edtrul.email === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
					// taken from $ Validate plugin
						filter = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
						if(!filter.test(val)) {return [false,nm+": "+$.jgrid.edit.msg.email,""];}
					}
				}
				if(edtrul.integer === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
						if(isNaN(val)) { return [false,nm+": "+$.jgrid.edit.msg.integer,""]; }
						if ((val % 1 !== 0) || (val.indexOf('.') != -1)) { return [false,nm+": "+$.jgrid.edit.msg.integer,""];}
					}
				}
				if(edtrul.date === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
						if(g.p.colModel[valref].formatoptions && g.p.colModel[valref].formatoptions.newformat) {
							dft = g.p.colModel[valref].formatoptions.newformat;
						} else {
							dft = g.p.colModel[valref].datefmt || "Y-m-d";
						}
						if(!$.jgrid.checkDate (dft, val)) { return [false,nm+": "+$.jgrid.edit.msg.date+" - "+dft,""]; }
					}
				}
				if(edtrul.time === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
						if(!$.jgrid.checkTime (val)) { return [false,nm+": "+$.jgrid.edit.msg.date+" - hh:mm (am/pm)",""]; }
					}
				}
				if(edtrul.url === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
						filter = /^(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
						if(!filter.test(val)) {return [false,nm+": "+$.jgrid.edit.msg.url,""];}
					}
				}
				if(edtrul.custom === true) {
					if( !(rqfield === false && $.jgrid.isEmpty(val)) ) {
						if($.isFunction(edtrul.custom_func)) {
							var ret = edtrul.custom_func.call(g,val,nm);
							if($.isArray(ret)) {
								return ret;
							} else {
								return [false,$.jgrid.edit.msg.customarray,""];
							}
						} else {
							return [false,$.jgrid.edit.msg.customfcheck,""];
						}
					}
				}
			}
			return [true,"",""];
		}
	});
})(jQuery);